/*
 * @Copyright: 2016 www.yyfax.com Inc. All rights reserved.
 */
package com.yyfax.pay.order.controller.api;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.exception.YYException;
import com.yyfax.framework.model.EmptyContent;
import com.yyfax.framework.model.YYFaxRsp;
import com.yyfax.framework.yyfax.CryptUtil;
import com.yyfax.framework.yyfax.enums.RspCodeEnum;
import com.yyfax.pay.common.enums.PayRspCodeEnum;
import com.yyfax.pay.common.enums.PaymentEnum;
import com.yyfax.pay.common.model.to.YYPayApiReq;
import com.yyfax.pay.config.data.service.BankConfigService;
import com.yyfax.pay.config.data.service.MerchantConfigService;
import com.yyfax.pay.config.model.po.MerchantConfigPO;
import com.yyfax.pay.order.data.service.UserAuthService;
import com.yyfax.pay.order.data.service.UserPaymentService;
import com.yyfax.pay.order.model.po.UserPaymentPO;
import com.yyfax.pay.order.model.to.QueryUserAuthContent;
import com.yyfax.pay.order.model.to.QueryUserAuthContent.UserCard;
import com.yyfax.pay.order.model.to.QueryUserAuthParams;
import com.yyfax.pay.order.model.to.RemoveUserPaymentParams;
import com.yyfax.pay.order.model.vo.UserAuthVO;
import com.yyfax.pay.order.model.vo.UserPaymentVO;

/**
 * 用户鉴权信息入口
 * 
 * @author buyi
 * @date 2017-04-18 14:32:51
 * @since v1.0.0
 */
@RestController
public class UserAuthApiController {
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private MerchantConfigService merchantConfigService;

	@Autowired
	private UserAuthService userAuthService;

	@Autowired
	private UserPaymentService userPaymentService;

	@Autowired
	private BankConfigService bankConfigService;

	/**
	 * 查询用户鉴权信息
	 * 
	 * @author buyi
	 * @date 2017-04-18 19:37:58
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/user_auth/query")
	public YYFaxRsp<QueryUserAuthContent> userAuthQuery(@RequestBody YYPayApiReq<QueryUserAuthParams> req) throws Throwable {
		logger.debug("enter user auth query ……");

		if (req == null || req.getParams() == null) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID);
		}

		QueryUserAuthParams params = req.getParams();

		// 1.校验参数
		params.validate();

		MerchantConfigPO merchantConfigPO = merchantConfigService.queryByMerchantCode(params.getMerchantCode());
		if (merchantConfigPO == null) {
			throw new YYException(PayRspCodeEnum.PAY_CODE_MERCHANT_NOT_EXISTS, "商户：" + params.getMerchantCode() + "信息不存在");
		}

		// 2.验签
		req.checkSign(merchantConfigPO.getSignKey());

		QueryUserAuthContent content = new QueryUserAuthContent();

		UserAuthVO userAuthVO = userAuthService.queryUserAuthVO(params.getMerchantCode(), params.getUserId());
		if (userAuthVO != null) {
			content.setMerchantCode(userAuthVO.getMerchantCode());
			content.setUserId(userAuthVO.getUserId());
			content.setIdNo(CryptUtil.decrypt(userAuthVO.getIdNoCip()));
			content.setName(userAuthVO.getName());

			List<UserPaymentVO> userPaymentVOs = userAuthVO.getUserPaymentVOs();
			if (userPaymentVOs != null) {
				List<UserCard> cards = new ArrayList<UserCard>();

				for (UserPaymentPO userPaymentPO : userPaymentVOs) {
					UserCard card = new UserCard();
					card.setPaymentCode(userPaymentPO.getPaymentCode());
					card.setPaymentName(PaymentEnum.fromValue(userPaymentPO.getPaymentCode()).getRemark());
					card.setCardNo(CryptUtil.decrypt(userPaymentPO.getCardNoCip()));
					card.setBankName(userPaymentPO.getBankName());

					cards.add(card);
				}

				content.setCards(cards);
			}
		}

		return new YYFaxRsp<QueryUserAuthContent>(PayRspCodeEnum.SUCCESS, content);
	}

	/**
	 * 商户用户支付通道信息
	 * 
	 * @author buyi
	 * @date 2017-04-18 19:54:48
	 * @since v1.0.0
	 * @param req
	 * @return
	 * @throws Throwable
	 */
	@RequestMapping(method = RequestMethod.POST, path = "/api/user_payment/remove")
	public YYFaxRsp<EmptyContent> userPaymentRemove(@RequestBody YYPayApiReq<RemoveUserPaymentParams> req) throws Throwable {
		logger.debug("enter user auth query ……");

		if (req == null || req.getParams() == null) {
			throw new YYException(RspCodeEnum.COMMON_ERROR_PARAMETER_INVALID);
		}

		RemoveUserPaymentParams params = req.getParams();

		// 1.校验参数
		params.validate();

		MerchantConfigPO merchantConfigPO = merchantConfigService.queryByMerchantCode(params.getMerchantCode());
		if (merchantConfigPO == null) {
			throw new YYException(PayRspCodeEnum.PAY_CODE_MERCHANT_NOT_EXISTS, "商户：" + params.getMerchantCode() + "信息不存在");
		}

		// 2.验签
		req.checkSign(merchantConfigPO.getSignKey());

		int i = userPaymentService.remove(params.getMerchantCode(), params.getUserId(), params.getPaymentCode(), params.getCardNo());

		if (i <= 0) {
			return new YYFaxRsp<EmptyContent>(PayRspCodeEnum.PAY_CODE_REMOVE_USER_PAYMENT_FAIL, "删除用户认证支付信息没有命中");
		}

		return new YYFaxRsp<EmptyContent>(PayRspCodeEnum.SUCCESS);
	}

}
